<?php
date_default_timezone_set('Asia/Shanghai');
mb_internal_encoding("UTF-8");

define('SITE_DIR', dirname(__FILE__));
define('BASE_DIR', dirname(SITE_DIR));
set_include_path('.' . PATH_SEPARATOR . get_include_path() 
. PATH_SEPARATOR . BASE_DIR. '/library/'
. PATH_SEPARATOR . BASE_DIR. '/application/models'
. PATH_SEPARATOR . BASE_DIR. '/application/views'
. PATH_SEPARATOR . BASE_DIR. '/application/controllers'
);

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

require('Xiachufang/util/Logger.php');
require('Xiachufang/util/MainConfig.php');
$configPath = BASE_DIR.'/application/config/config.php';
Logger::configure($configPath);
MainConfig::init($configPath);
//set_exception_handler('handle_exception');

require_once 'Zend/Controller/Front.php';
$ctrl = Zend_Controller_Front::getInstance(); 
$ctrl->setControllerDirectory(array('default'=>BASE_DIR.'/application/controllers'));
$ctrl->throwExceptions(true); 
$ctrl->setParam('noViewRenderer', true); 

$config = new Zend_Config_Ini(BASE_DIR.'/application/config/router.ini', 'rewrite');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($config, 'routes');
$ctrl->setRouter($router);

$ctrl->dispatch();



function handle_exception(Exception $e) {
    try {
    	Logger::getLogger('xiachufang')->fatal($msg."\n".$e->getTraceAsString(),$msg);
//    	$loader->showErrorMessage($msg.'&nbsp;&nbsp;<a href="javascript:history.go(-1);">返回</a>');
    }catch (Exception $ex) {
    	var_dump($e->getTraceAsString());
        Logger::getLogger('xiachufang')->warn($e->getTraceAsString());
    }
}

function notFound() {
	header("http/1.1 404 not found");
	exit();
}

function strlen2($str) {
	if (!$str) return 0;
	return strlen(preg_replace("/[\x{4e00}-\x{9fa5}]/u","aa", $str));
}

function alert($str) {
	$smarty = new BaseView();
	$smarty->assign(array(
		"pageTitle"=>"出错了",
		"msg"=>$str,
	));
	$smarty->display("include/error.tpl");
	//print '<script type="text/javascript">alert("'.$str.'");history.back();</script>';
	exit();
}
?>
